﻿using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace StuClassInfo
{
    public partial class StuInfo : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                StuRepeat();
                Response.Write("<script>alert('hello world')</script>");
            }
        }

        private void StuRepeat()
        {
            //方法1
            #region EF：显示数据
            //1.实例化数据库访问上下文
            using (var db = new StuUseEntities())
            {
                //2.根据ID查询实体

                //接口IQuerying
                List<UserInfo> list = db.UserInfo.Where(s => true).ToList();

                //延时加载 实现延时加载接口IEnumerable 
                //var models = db.UserInfo.Where(s => true);
                //var list = list.ToList();

                List<StuModel> listmodel = new List<StuModel>();
                list.ForEach(s =>
                {
                    var model = new StuModel()
                    {
                        userid = s.userid,
                        username = s.username,
                        userage = s.userage,
                        userphone = s.userphone,
                        userEmail = s.userEmail,
                        classid = (int)s.classid,
                        classname = s.StuClass.classname,
                    };
                    listmodel.Add(model);
                });
                Repeater_stu.DataSource = listmodel;
                Repeater_stu.DataBind();
            }
            #endregion

            //方法2
            #region 
            //using (StuUseEntities db = new StuUseEntities())
            //{
            //    DbSet<UserInfo> list = db.Set<UserInfo>();
            //    var stu = from s in db.UserInfo
            //              join c in db.StuClass on s.classid equals c.classid
            //              select new { s.userid, s.username, s.userage, s.userphone, s.userEmail, c.classname };
            //    Repeater_stu.DataSource = stu.ToList();
            //    Repeater_stu.DataBind();
            //}
            #endregion

        }
    }
}